#ifndef MEASURE616RECTEXTRACT_H
#define MEASURE616RECTEXTRACT_H

#include <opencv2/opencv.hpp>
#include <cmath>
#define PI 3.1415926

using namespace std;

class MeasurE616RectExtract
{
public:
    MeasurE616RectExtract();
    //用kirsch边缘提取求直线边缘，用线扫方式
    int KirschEdgeLine(cv::Mat srcimg,int threshold,int orientation, vector<cv::Point>& contours);
    //用kirsch边缘提取求弧边缘，用旋转射线方式
    int KirschEdgeCircle(cv::Mat srcimg,int threshold, int startangle, int endangle, vector<cv::Point>& contours);
    //计算小圆边缘
    int GetSmallCircle(cv::Mat srcimg,int threshold, int num, vector<float>& result);
    vector<float> FitCircle(vector<cv::Point>& contours);
    int GetRectEdge(cv::Mat srcimg,int threshold,int num, vector<float>& dists);
    int KirschEdgeOuter(cv::Mat srcimg, int thresh, int num, float& dist);
    //找基准
    int GetDatum(cv::Mat srcimg,vector<cv::Point2f>& datum);
    //点到直线距离
    float Point2Line(cv::Point Point, vector<cv::Point2f> datum, int flag);//0:与左边基准的距离;1:与上边基准的距离

private:
    //卷积
    void conv2D(cv::InputArray _src, cv::InputArray _kernel, cv::OutputArray _dst, int ddepth, cv::Point anchor = cv::Point(-1, -1), int borderType = cv::BORDER_DEFAULT);
    //krisch边缘检测
    cv::Mat krisch(cv::InputArray src,int borderType = cv::BORDER_DEFAULT);
    //判断点周围密度
    int PointDense(cv::Mat srcimg, cv::Point center, int rad, float thresh);
    //截取边缘
    vector<cv::Point> ContoursCut(vector<cv::Point> contours,int startangle,int endangle);
    //用中心点加旋转射线方法找边缘
    int FindEdge(cv::Mat srcimg, cv::Point seed, vector<cv::Point>& contours, int radius, float percent, int radthresh);
    //拟合圆
    void GetCircle(vector<cv::Point> contours, cv::Point center, int threshold,cv::Vec3f& circlepara);
    //计算圆上中点坐标 0：上面 1：下面
    cv::Point GetTanPoint(int a, int b, int R, float k, int flag);
    //计算差距
    double CalcDiff(vector<cv::Point> contours,cv::Point center);
    //截取片段
    vector<cv::Point> GetSegments(vector<cv::Point> coontours, vector<int> segments);
    int KirschEdge(cv::Mat srcimg,int threshold, vector<cv::Point>& contours, vector<vector<cv::Point>>& linecontours, vector<int> segment);
    void FitLineAndGetDist(vector<vector<cv::Point>> linecontours,vector<int> coordinates,vector<float>& dist);
    void ScanRightLine(cv::Mat srcimg, vector<cv::Point>& contours);
    bool PixelDense(cv::Mat srcimg, int thresh);
    void LineFitLeastSquares(vector<cv::Point> contours, vector<float> &vResult);
    void GetSeedPoints(cv::Mat srcimg,int orientation,int startline,int endline,int threshold, vector<cv::Point>& seedpoints);
    void GetEdgePoint(cv::Mat srcimg, vector<cv::Point> seedpoints, int orientation, int thresh,int radius,float percent,  vector<cv::Point>& contours);
    int Pixel2Curve(cv::Mat srcimg,int startpos, int endpos, int orientation, vector<vector<float>>& curves);
    void GetSeedPos(vector<vector<float>> curves, int thresh, vector<int>& seedpoints);
    void GetSrcEdgePoints(cv::Mat srcimg,int orientation,int startline,int endline,int threshold, vector<cv::Point>& edgepoints);
    void GetSeedPointsSec(cv::Mat srcimg,int orientation,int startline,int endline,int threshold, vector<cv::Point>& seedpoints);
    void GetSeedPosSecond(vector<vector<float>> curves,int thresh, vector<int>& seedpoints);
    //已知方向，通过从一边线扫求边缘
    int ScanLine(cv::Mat srcimg,int lineori, vector<cv::Point>& contours,int startline,int radius,float percent);//lineori: 1:top,2:right,3:bottom,4:left
    //中分线Y轴坐标fai53
    float GetMidLineY(vector<vector<cv::Point>> linecontours, int pos);
    //线X坐标fai52
    float GetLineX(vector<cv::Point> linecontour, int pos);
    //计算小圆边缘
    int KirschEdgeSmallCircle(cv::Mat srcimg,int threshold, int num, vector<cv::Point>& contours);
};

#endif // MEASURE616RECTEXTRACT_H
